import random
import string
from django.core.management.base import BaseCommand
from hostmanager.models import Host

class Command(BaseCommand):
    help = 'Change root passwords for all hosts'

    def handle(self, *args, **kwargs):
        for host in Host.objects.all():
            new_password = ''.join(random.choices(string.ascii_letters + string.digits, k=12))
            host.root_password = new_password
            host.save()
            self.stdout.write(self.style.SUCCESS(f'Successfully changed password for {host.ip_address}'))